*cd "/Users/vetsean/Google Drive/Issue Voting measurement error/Main findings"

version 12.1
ssc install  tuples

*************************************************
***** Case Study Figure 1 Economic Scale in the ANES 1972-76*************************
global factor 1 // 1 = factor scores vs simple average
global standardized 1 // 1 = means zero, standard deviation one
global missing = . // Set missing value. All coded to 7-point scales, so 4 is midpoint
                     // Need to remain consistent about that when adding new panels
global ktype 1 // 1 normal knowledge coding, 9 place them on the right sides of the scale 
global studies Econ_SSI1516  All_92_96 Econ_7276  Econ_BES9295 Econ_BES9296 Econ_BES9297 Econ_BES9701 Econ_76 Econ_94_96 
do stability_pooled_coding_1_3.do // multi-item scale stability data set - takes a while to run
*figure 1a - number of items in scale
use Data/stability_pooled_corr_ARS_std${standardized}ktype${ktype}fct${factor} if study == "Econ_7276", clear
graph box  corr , yt("Correlation between wave 1 and 2",size(medium))  over(ni,lab( labsize(medium))) ///
  scheme(lean2) yl(0.2(0.2)0.8, nogrid) b2("Number of items in the scale",size(medium))
graph export Output\figure1a.png,width(3200)  height(2400)  replace
*figure 1b
use Data/stability_pooled_std${standardized}ktype${ktype}fct${factor} if NUM_issues == 4 & study == "Econ_7276" , clear
g temporary = ""
for num 1/5: replace temporary = "GK Quintile = X" if gk_5 ==X
scatter scale_2 scale_1, ///m(i) ml(PID_label) mlabpos(0) mlabs(small) 
  by(temporary, note("") subtitle("ANES 1972 & 1976"))  scheme(lean2) yl(-2(2)2, nogrid) ///
	  xt("Economic Issues Wave 1",si(medium))  yt("Economic Issues Wave 2",si(medium)) 
graph export  Output/figure1b.png,width(3200)  height(2400)  replace
bys temporary: corr scale_2 scale_1 
*figure 1c  
use Data/stability_pooled_std${standardized}ktype${ktype}fct${factor} if NUM_issues == 4 & study == "Econ_7276" , clear
g temporary = ""
for num 0/4: replace temporary = "Correctly Place = X" if total_knowledge ==X
scatter scale_2 scale_1  , ///m(i) ml(PID_label) mlabpos(0) mlabs(small) 
  by(temporary, note("") subtitle("ANES 1972 & 1976"))  scheme(lean2) yl(-2(2)2, nogrid) ///
  xt("Economic Issues Wave 1",si(medium))  yt("Economic Issues Wave 2",si(medium)) 
graph export Output/figure1c.png,width(3200)  height(2400)  replace
bys temporary:  corr scale_2 scale_1

*********************************************************************************************
**Table 1
*********************************************************************************************
*this Excel file combine these into table 1:
*Paper and SI\figures and tables\stability correlations table.xlsx
global factor 1 // 1 = factor scores vs simple average
global standardized 1 // 1 = means zero, standard deviation one
global missing = . // Set missing value. All coded to 7-point scales, so 4 is midpoint
                     // Need to remain consistent about that when adding new panels
global ktype 1 // 1 normal knowledge coding, 9 place them on the right sides of the scale 
global studies All_92_96 Econ_7276  Econ_BES9295 Econ_BES9296 Econ_BES9297 Econ_BES9701 Econ_76 Econ_94_96 
do stability_pooled_coding_1_3.do // multi-item scale stability data set - no need to run if you did so above
capture file close myfile
****Table 1a - correlations by number of items
file open myfile using Output/table1a_corrsNum_std${standardized}ktype${ktype}fct${factor}.txt, write replace 
for num 0/10: file write myfile _tab "X"
   file write myfile  _n
foreach s in $studies { 
  use Data/stability_pooled_std${standardized}ktype${ktype}fct${factor} if study == "`s'", clear	
  sum scale_1 scale_2
  sum NUM_issues
  local total = r(max)
  file write myfile "`s'" _tab 
  for num 1/`total': capture corr scale_2 scale_1 if NUM_issues==X \ capture file write myfile %7.2f (r(rho)) _tab  
   file write myfile  _n
}
file close myfile
***Table 1b - correlations by general knowledge
capture file close myfile
file open myfile using Output/table1b_gk_${standardized}ktype${ktype}fct${factor}.txt, write replace 
for num 1/5: file write myfile _tab "X"
   file write myfile  _n
foreach s in $studies { 
  use Data/stability_pooled_std${standardized}ktype${ktype}fct${factor} if study == "`s'", clear	
  egen ni_max = max(NUM_issues), by(study) 
  keep if ni_max ==NUM_issues
  display "`s'"
  file write myfile "`s'" _tab 
  for num 1/5: capture corr scale_2 scale_1 if gk_5==X \ capture file write myfile %7.2f (r(rho)) _tab  
   file write myfile  _n
}
file close myfile
  use Data/stability_pooled_std${standardized}ktype${ktype}fct${factor} , clear	

tab study gk_5
***Table 1c - correlations by placement knowledge             ******************
capture file close myfile
file open myfile using Output/table1c_corrstk_std${standardized}ktype${ktype}fct${factor}.txt, write replace 
for num 0/10: file write myfile _tab "X"
   file write myfile  _n
foreach s in $studies { 
  use Data/stability_pooled_std${standardized}ktype${ktype}fct${factor} if study == "`s'", clear	
  egen ni_max = max(NUM_issues), by(study) 
  keep if ni_max ==NUM_issues
  display "`s'"
  sum scale_1 scale_2
  sum NUM_issues
  local total = r(max)
  file write myfile "`s'" _tab 
  for num 0/`total': capture corr scale_2 scale_1 if total_knowledge==X \ capture file write myfile %7.2f (r(rho)) _tab  
   file write myfile  _n
}
file close myfile
*this Excel file combine these into table 1:
*Paper and SI\figures and tables\stability correlations table.xlsx



*************************************************************
********Table 2  of percentage and N by placement knowledge             ******************
*************************************************************SSI1516
global studies All_92_96 Econ_7276  Econ_BES9295 Econ_BES9296 Econ_BES9297 Econ_BES9701 Econ_76 Econ_94_96 
capture file close myfile
file open myfile using Output/Table_2.txt, write replace 
for num 0/10: file write myfile _tab "X"
   file write myfile  _n
foreach s in $studies { 
  use Data/stability_pooled_std${standardized}ktype${ktype}fct${factor} if study == "`s'", clear	
  egen ni_max = max(NUM_issues), by(study) 
  keep if ni_max ==NUM_issues
  display "`s'"
  sum scale_1 scale_2
  local totalN=r(N)
  display `totalN'
  sum NUM_issues
  local total = r(max)
  file write myfile "`s'" _tab 
  for num 0/`total': capture corr scale_2 scale_1 if total_knowledge==X \ capture file write myfile %7.0f (r(N)/`totalN'*100) _tab  
  *for num -1/6: capture corr scale_2 scale_1 if total_knowledge==X \ capture file write myfile %7.2f (r(rho)) _tab  
   file write myfile  _n
}
file close myfile

  
  
*************************************************************
********Figure 2 individual-item stability dot plot with correlations
*************************************************************SSI1516
global factor 0 // 1 = factor scores vs simple average
global standardized 0 // 1 = means zero, standard deviation one
global missing = . // Set missing value. All coded to 7-point scales, so 4 is midpoint
                     // Need to remain consistent about that when adding new panels
global ktype 1 // 1 normal knowledge coding, 9 place them on the right sides of the scale  
global studies Econ_SSI1516  All_92_96   Econ_7276  Econ_BES9295 Econ_BES9296 Econ_BES9297 Econ_BES9701 Econ_76 Econ_94_96  
do stability_ind_coding_0_1.do
capture file close myfile
use  Data/stability_ind_std${standardized}ktype${ktype}fct${factor}, clear
tab k_scale
tab study issues_scale
sum scale_1 scale_2 total_knowledge  k_scale
tempname memhold
postfile `memhold' t corr N ni tk str17 study str17 issue  using Data/stability_ind_corr_tk, replace	
foreach s in $studies {
	disp "`s'"
    sum t if study == "`s'"
    local z = r(max)
	sum total_knowledge if study == "`s'"
	local u = r(max)
	forval i=1/`z' { // tupples
	  forval l=0/1 { // total issue knowledge
			  sum NUM_issues if k_scale == `l'  & t==`i' & study == "`s'"
			  local w = r(mean)
				tempvar tv1
				gene `tv1'=_n if k_scale == `l'  & t==`i' & study == "`s'"
				summ `tv1', meanonly
				local index=r(min)
				local mymacro=issues_scale[`index']
				drop `tv1'
			  capture corr scale_1 scale_2 if  k_scale == `l' & t==`i' & study == "`s'"
			   if _rc==0 {
				 post `memhold' (`i') (r(rho) ) (r(N) ) (`w') (`l') ("`s'") ("`mymacro'") 
			   }
		   }
	}
}
postclose `memhold'

use  Data/stability_ind_corr_tk, clear
 replace study =regexr(study,"Econ_","")
 replace study =regexr(study,"All_","")
drop if study =="BES9295" 
drop if study =="BES9296" 
replace study ="ANES9296" if study== "92_96"
replace study ="ANES9296" if study== "92_96"
replace study ="ANES9496" if study== "94_96"
replace study ="ANES7276" if study== "7276"
replace study ="PAT76" if study== "76"
tab study
reshape wide corr N,i(issue study) j(tk)
*drop if sample size is under 5 for either group
*Patterson questions are slightly different
tab issue if study=="PAT76"
replace issue = "Government jobs" if  issue=="jobs" & study=="PAT76"
replace issue = "Price controls" if issue=="price" & study=="PAT76"
replace issue = "Tax cut low incomes" if issue=="tax" & study=="PAT76"
replace issue = "Social welfare spending" if  issue=="spd" & study=="PAT76"
replace issue = "Taxes on high incomes" if issue=="tax"
replace issue = "Abortion" if issue=="abr"
replace issue = "Aid to Blacks" if issue=="aid"
replace issue = "Childcare spending" if issue=="childcare"
replace issue = "Debt ceiling" if issue=="debtceiling"
replace issue = "European integration" if issue=="eci"
replace issue = "Defense spending" if issue=="def"
replace issue = "Food stamps" if issue=="foodstamps"
replace issue = "Gov. services" if issue=="gss" || issue=="services" 
replace issue = "Food stamps" if issue=="foodstamps"
replace issue = "Equalize incomes" if issue=="ieq"
replace issue = "Nationalize industry" if issue=="prn"
replace issue = "Tax and spend more" if issue=="txs"
replace issue = "Employment vs. infl." if issue=="jbp"
replace issue = "Housing spending" if issue=="housing"
replace issue = "Busing" if issue=="bus"
replace issue = "Universal health care" if issue=="health"
replace issue = "Ideology" if issue=="ide"
replace issue = "Guaranteed jobs" if issue=="job" || issue=="jobs"
replace issue = "Medicare spending" if issue=="medicare"
replace issue = "Raise minimum wage" if issue=="minwage"
replace issue = "Parental leave" if issue=="parentleave"
replace issue = "Price controls" if issue=="price"
replace issue = "Marijuana legalization" if issue=="pot"
replace issue = "Rights of accused criminals" if issue=="rts"
replace issue = "School spending" if issue=="schools"
replace issue = "Social Security" if issue=="ss"
replace issue = "Taxes on high incomes" if issue=="tax"
replace issue = "Women's rights" if issue=="wmn"
tab issue
g issue_study = issue +" (" +study+")" 
g difference = corr1-corr0
 destring study, replace
save  Data/stability_ind_corr_tk2, replace
*Figure 2 produced by Figures_2_3.R (after running the code above)

********************************************************************************
   * N for Figure 2
   use  Data/stability_ind_corr_tk2, clear
    egen total = rowtotal(N*)
    egen total_N = total(total)
	sum total_N // Number of respondents
    egen total_issues = group(issue_study)
	sum total_issues // Number of issues
	tabulate issue_study
    use  Data/stability_ind_std${standardized}ktype${ktype}fct${factor}, clear
     replace study =regexr(study,"Econ_","")
     replace study =regexr(study,"All_","")
    drop if study =="BES9295" 
    drop if study =="BES9296" 
	sum scale_1 scale_2 agree k_scale PID_1
	egen total_respondent = group(ID)
	sum total_respondent //maximum gives you the total number of respondents
	 egen total_N =count(scale_2)
	 sum total_N // Number of respondents
	  g issue_study = issues_scale +" (" +study+")" 
      tabulate study issues_scale
	  	egen temporary = tag(total_respondent)
	egen total_respondent_study = total(temporary), by(study)
	sum total_respondent_study 
      tab study, sum(total_respondent_study)
  *is the variance larger?
  	use Data/stability_ind_std${standardized}ktype${ktype}fct${factor} if PID_1!=.5 & PID_2!=.5 , clear	
     table agree k_scale, c(sd scale_1 sd scale_2)   f(%7.2f)




		
**************************************************************
******** Table 3 summarizing findings for crystallized and absolute change
global factor 0 // 1 = factor scores vs simple average
global standardized 0 // 1 = means zero, standard deviation one
global missing = . // Set missing value. All coded to 7-point scales, so 4 is midpoint
                     // Need to remain consistent about that when adding new panels
global ktype 1 // 1 normal knowledge coding, 9 place them on the right sides of the scale 
do stability_pooled_coding_1_3.do // multi-item scale stability data set
use Data/stability_pooled_std${standardized}ktype${ktype}fct${factor}  , clear
   egen DK_total_1=sum( missing_1), by(ID) // total DK on self-placement no DK for multi-item scales so calculating this only for single item scales and then applying it to the same individuals
   egen DK_total_2=sum( missing_2), by(ID) // total DK on self-placement
   egen DK_total = rowtotal(DK_total_1 DK_total_2)
      tab study DK_total
egen N= count(scale_1), by(study)
g N_standard =1/N*1000
replace N_standard = N_standard/3 if regexm(study, "BES92")
capture drop crystallized
g crystallized = (scale_1>4 & scale_2 > 4 &scale_1<. & scale_2 <. | scale_1<4 & scale_2 <4& scale_1<. & scale_2 <.) & DK_total== 0
keep if NUM_issues== total_issues // must be below DK total and crystallized commands
g keep =  DK_total_1 < total_issues *0.754 & DK_total_2 < total_issues *0.754
sum  scale_1 scale_2 crystallized change
bys study:sum  crystallized 
replace k_percent =k_percent*total_issues*2
*number of unique respondents
sort study ID
egen number_ID = group(ID) if keep == 1
sum number_ID // number of unique respondents
bys study:sum number_ID ID
bys study:sum number_ID
tab issues
egen number_item = group(issues_scale study)
sum number_item
bys study:sum ID DK_total_1 DK_total_2
sum crystallized change
sum crystallized change if DK_total_1 < total_issues *0.754 & DK_total_2 < total_issues *0.754
bys study:sum crystallized keep k_scale
bys study:sum  keep 
capture file close myfile 
file open myfile using Output/table_3.rtf, write replace
file write myfile %9s "Individual items pooled"  _tab  _tab   _tab  _tab  _tab   _tab  _tab  _tab   _tab  _tab    _n 
*basic result gk
file write myfile %9s "DV"        _tab "Crystallized" _tab      _tab _tab  _tab  _tab "Change" _tab   _tab  _tab  _tab _n 
file write myfile %9s "Treatment" _tab "GK"           _tab "PC" _tab "p-value"  _tab "R2" _tab "N"  _tab "GK" _tab "PC"   _tab "p-value" _tab "R2" _tab "N"  _n 
file write myfile %9s "Multi-item scales pooled"  _n 
sum  crystallized gk  study_n* gk 
  regress crystallized   study_n* gk  [aw=N_standard] , cluster(ID)
  	file write myfile %9s "  General political knowledge only (0-1)" _tab %4.2f (_b[gk])  "(" %4.2f (_se[gk]) ")"  _tab   _tab _tab %4.2f (e(r2))   _tab %3.0f (e(N)) 
  regress change  study_n* gk  if keep == 1    [aw=N_standard], cluster(ID)
  	file write myfile  _tab %4.2f (_b[gk])  "(" %4.2f (_se[gk]) ")"  _tab  _tab   _tab %4.2f (e(r2))   _tab %3.0f (e(N)) _n 
*basic result placement knowledge
  regress crystallized study_n* k_percent [aw=N_standard], cluster(ID)
  	file write myfile %9s "  Percent placed correctly only (0/1)" _tab  _tab %4.2f (_b[k_percent]) "(" %4.2f (_se[k_percent]) ")"  _tab   _tab %4.2f (e(r2))   _tab %3.0f (e(N)) 
  regress change       study_n* k_percent if keep == 1 [aw=N_standard], cluster(ID)
  	file write myfile   _tab  _tab %4.2f (_b[k_percent]) "(" %4.2f (_se[k_percent]) ")"  _tab  _tab %4.2f (e(r2))   _tab %3.0f (e(N)) _n 
*basic result with both
  regress crystallized study_n* gk k_percent [aw=N_standard], cluster(ID)
    test gk =k_percent 
  	file write myfile %9s "  Both" _tab %4.2f (_b[gk])  "(" %4.2f (_se[gk]) ")"  _tab %4.2f (_b[k_percent]) "(" %4.2f (_se[k_percent]) ")" _tab  %4.2e (r(p)) _tab %4.2f (e(r2))   _tab %3.0f (e(N)) 
  regress change       study_n* gk  k_percent if keep == 1 [aw=N_standard], cluster(ID)
    test gk =k_percent 
  	file write myfile  _tab %4.2f (_b[gk])  "(" %4.2f (_se[gk]) ")"  _tab %4.2f (_b[k_percent]) "(" %4.2f (_se[k_percent]) ")"  _tab  %4.2e (r(p)) _tab %4.2f (e(r2))   _tab %3.0f (e(N)) _n 
********individual-level
*stability_coding_ind.do defines the crystallized variable 
global factor 0 // 1 = factor scores vs simple average
global standardized 0 // 1 = means zero, standard deviation one
global missing = . // Set missing value. All coded to 7-point scales, so 4 is midpoint
global ktype 1 // 1 normal knowledge coding, 9 place them on the right sides of the scale  
*do stability_ind_coding_0_1.do // 
use Data/stability_ind_std${standardized}ktype${ktype}fct${factor}  , clear	
   sum  scale_1 scale_2 crystallized change gk k_percent
egen N= count(scale_1), by(study)
g N_standard =1/N*1000
replace N_standard = N_standard/3 if regexm(study, "BES92")
tab  N_standard study
tab study total_issues
 tabulate issue
 sum  crystallized change  gk   k_scale 
 replace  k_scale=k_scale*2
bys study: sum k_scale gk
*number of unique respondents
sort study ID
egen number_ID = group(ID)
sum number_ID
bys study:sum number_ID
bys study:sum number_ID
bys study:sum missing_1 missing_2
tab issues
egen number_item = group( study)
sum number_item
file write myfile %9s ""  _tab  _tab   _tab  _tab  _tab   _tab  _tab  _tab   _tab  _tab   _n 
file write myfile %9s "Individual items pooled"  _tab  _tab   _tab  _tab  _tab   _tab  _tab  _tab   _tab  _tab   _n 
*basic result gk
  regress crystallized    i.study_issue gk  [aw=N_standard], cluster(ID)
  	file write myfile %9s "  General political knowledge (0-1)" _tab %4.2f (_b[gk])  "(" %4.2f (_se[gk]) ")"  _tab  _tab   _tab  %4.2f (e(r2))   _tab %3.0f (e(N)) 
  regress change  gk  i.study_issue    [aw=N_standard], cluster(ID)
  	file write myfile  _tab %4.2f (_b[gk])  "(" %4.2f (_se[gk]) ")"  _tab  _tab   _tab %4.2f (e(r2))   _tab %3.0f (e(N)) _n 
*basic result placement knowledge
  regress crystallized    i.study_issue  k_scale [aw=N_standard], cluster(ID)
  	file write myfile %9s "  Placed correctly (0/1)" _tab  _tab %4.2f (_b[k_scale]) "(" %4.2f (_se[k_scale]) ")"  _tab  _tab   _tab %3.0f (e(N)) 
  regress change  i.study_issue  k_scale  [aw=N_standard], cluster(ID)
  	file write myfile   _tab  _tab %4.2f (_b[k_scale]) "(" %4.2f (_se[k_scale]) ")"  _tab   _tab %4.2f (e(r2))   _tab %3.0f (e(N)) _n 
*basic result with both
  regress crystallized    i.study_issue gk k_scale [aw=N_standard], cluster(ID)
  test gk =k_scale
  	file write myfile %9s "Both" _tab %4.2f (_b[gk])  "(" %4.2f (_se[gk]) ")"  _tab %4.2f (_b[k_scale]) "(" %4.2f (_se[k_scale]) ")"  _tab  %4.2e (r(p)) _tab %4.2f (e(r2))   _tab %3.0f (e(N)) 
  regress change  gk  i.study_issue  k_scale  [aw=N_standard], cluster(ID)
    test gk =k_scale
	file write myfile  _tab %4.2f (_b[gk])  "(" %4.2f (_se[gk]) ")"  _tab %4.2f (_b[k_scale]) "(" %4.2f (_se[k_scale]) ")"  _tab  %4.2e (r(p)) _tab %4.2f (e(r2))   _tab %3.0f (e(N)) _n 
*respondent fixed effects sample
file write myfile %9s "Fixed effects"  _tab  _tab   _tab  _tab  _tab   _tab  _tab  _tab   _tab  _tab   _n 
  regress crystallized    i.study_issue gk k_scale if placement_percentnr!=0 &  placement_percentnr!=100  [aw=N_standard], cluster(ID) 
    test gk =k_scale
  	file write myfile %9s "  Resp. fixed effects sample" _tab %4.2f (_b[gk])  "(" %4.2f (_se[gk]) ")"  _tab %4.2f (_b[k_scale]) "(" %4.2f (_se[k_scale]) ")"   _tab %4.2e (r(p))  _tab %4.2f (e(r2))   _tab %3.0f (e(N)) 
  regress change  gk  i.study_issue  k_scale gk if placement_percentnr!=0  &  placement_percentnr!=100  [aw=N_standard], cluster(ID)
  	test gk =k_scale
	file write myfile                                  _tab %4.2f (_b[gk])  "(" %4.2f (_se[gk]) ")"  _tab %4.2f (_b[k_scale]) "(" %4.2f (_se[k_scale]) ")"   _tab %4.2e (r(p)) _tab %4.2f (e(r2))   _tab %3.0f (e(N)) _n 
*respondent fixed effects 
  areg crystallized    i.study_issue gk k_scale  if placement_percentnr!=0 &  placement_percentnr!=100  [aw=N_standard], cluster(ID)  a(ID)
	file write myfile %9s "  Resp. fixed effects" _tab  "--"  _tab %4.2f (_b[k_scale]) "(" %4.2f (_se[k_scale]) ")"   _tab _tab %4.2f (e(r2))   _tab %3.0f (e(N)) 
  areg change    i.study_issue gk k_scale  if placement_percentnr!=0 &  placement_percentnr!=100  [aw=N_standard], cluster(ID) a(ID)
  	file write myfile                           _tab "--"  _tab %4.2f (_b[k_scale]) "(" %4.2f (_se[k_scale]) ")"  _tab  _tab %4.2f (e(r2))   _tab %3.0f (e(N)) _n 
	xtset ID
	xtsum crystallized
	xtsum change
*partisans untreated
capture drop treatment
g treatment =k_scale 
replace treatment =0 if PID_1==.5  & therm_1==.5
  areg crystallized    i.study_issue gk treatment if placement_percentnr!=0 &  placement_percentnr!=100  [aw=N_standard], cluster(ID)  a(ID)
  	file write myfile %9s "  Plus independents, other party, neutral candidate/party preference coded to 0 on treatment" _tab  "--"   _tab %4.2f (_b[treatment]) "(" %4.2f (_se[treatment]) ")"   _tab _tab %4.2f (e(r2))   _tab %3.0f (e(N)) 
  areg change       i.study_issue gk treatment if placement_percentnr!=0 &  placement_percentnr!=100  [aw=N_standard], cluster(ID)  a(ID)
  	file write myfile  _tab  "--"  _tab %4.2f (_b[treatment]) "(" %4.2f (_se[treatment]) ")"   _tab _tab %4.2f (e(r2))   _tab %3.0f (e(N)) _n 	
*agree with your party or candidate
replace treatment =0 if agree == 0
  areg crystallized    i.study_issue gk treatment if placement_percentnr!=0 &  placement_percentnr!=100  [aw=N_standard], cluster(ID)  a(ID)
  	file write myfile %9s "  Plus agree with your party or candidate" _tab  "--"   _tab %4.2f (_b[treatment]) "(" %4.2f (_se[treatment]) ")"   _tab _tab %4.2f (e(r2))   _tab %3.0f (e(N)) 
  areg change       i.study_issue gk treatment if placement_percentnr!=0 &  placement_percentnr!=100  [aw=N_standard], cluster(ID)  a(ID)
  	file write myfile  _tab  "--"  _tab %4.2f (_b[treatment]) "(" %4.2f (_se[treatment]) ")"   _tab _tab %4.2f (e(r2))   _tab %3.0f (e(N)) _n 	
*stable party and candidate preferences
replace treatment =0 if PID_1!=PID_2 | therm_switch==1
  areg crystallized    i.study_issue gk treatment if placement_percentnr!=0 &  placement_percentnr!=100  [aw=N_standard], cluster(ID)  a(ID)
  	file write myfile %9s "  Plus party changers candidate changers coded to 0 on treatment" _tab  "--"   _tab %4.2f (_b[treatment]) "(" %4.2f (_se[treatment]) ")"   _tab _tab %4.2f (e(r2))   _tab %3.0f (e(N)) 
  areg change       i.study_issue gk treatment if placement_percentnr!=0 &  placement_percentnr!=100  [aw=N_standard], cluster(ID)  a(ID)
  	file write myfile  _tab  "--"  _tab %4.2f (_b[treatment]) "(" %4.2f (_se[treatment]) ")"   _tab _tab %4.2f (e(r2))   _tab %3.0f (e(N)) _n 	
file close myfile 

*are people above chance levels ?
table k_scale, c(mean crystallized)
table k_scale if PID_1==PID_2 , c(mean crystallized)
table k_scale if PID_1==PID_2 & therm_switch==0 , c(mean crystallized)


 
********Figure 3 individual-item stability dot plot
***Place positions correctly on single item** Table 3 left-sided
*agree placement knowledge and placement correct for NUM_issue== 1& PID_1 == PID_2
  *must be unstandardized
global factor 0 // 1 = factor scores vs simple average
global standardized 0 // 1 = means zero, standard deviation one
global missing = . // Set missing value. All coded to 7-point scales, so 4 is midpoint
                     // Need to remain consistent about that when adding new panels
global ktype 1 // 1 normal knowledge coding, 9 place them on the right sides of the scale  
*do stability_pooled_coding_1_2.do // multi-item scale stability data set
*use Data/stability_pooled_std0ktype1fct0 if NUM_issue == 1 & PID_1!=.5 & therm_1!=0.5 & PID_2!=.5 & therm_2!=.5, clear	
do stability_ind_coding_0_1.do // multi-item scale stability data set
use Data/stability_ind_std${standardized}ktype${ktype}fct${factor} if PID_1!=.5 & PID_2!=.5 , clear	
egen N= count(scale_1), by(study)
g N_standard =1/N*1000
replace N_standard = N_standard/3 if regexm(study, "BES92")
*what percent of people agree
*among all partisans
tab agree k_scale , col
tab agree k_scale [aw=N_standard], col
*among all partisans who also prefer their party's leader
tab agree k_scale if (PID_1 > .5 & therm_1>.5 | PID_1 < .5 & therm_1<.5) &PID_1 < . & therm_1<.&(PID_2 > .5 & therm_2>.5 | PID_2 < .5 & therm_2<.5) &PID_1 < . & therm_2<., col
tab agree k_scale if (PID_1 > .5 & vote_1>.5 | PID_1 < .5 & vote_1<.5) &PID_1 < . & vote_1<.&(PID_2 > .5 & vote_2>.5 | PID_2 < .5 & vote_2<.5) &PID_1 < . & vote_2<., col
tab agree k_scale if scale_1!= 4 &(PID_1 > .5 & vote_1>.5 | PID_1 < .5 & vote_1<.5) &PID_1 < . & vote_1<.&(PID_2 > .5 & vote_2>.5 | PID_2 < .5 & vote_2<.5) &PID_1 < . & vote_2<., col
tab agree k_scale if scale_1!= 4 &(PID_1 > .5 & therm_1>.5 | PID_1 < .5 & therm_1<.5) &PID_1 < . & therm_1<. [aw=N_standard], col
*among all partisans who take a side
tab agree k_scale if scale_1!= 4, col
tab agree k_scale if scale_1!= 4 [aw=N_standard], col
 g issue_study = issues_scale +" (" +study+")" 
table issue_study k_scale  [aw=N_standard],c(mean agree) f(%7.2f) row
table issue_study k_scale  if scale_1!= 4 [aw=N_standard],c(mean agree) f(%7.2f) row
 tabulate issue_study
****loop over specific issue knowledge****
tab1 k_scale agree 
tempname memhold
postfile `memhold'  corr N  k str17 study agree str17 issue sd1 sd2 issue_number using Data/stability_pooled_corr_kak_std0ktype1fct0, replace	
foreach s in $studies {
    sum t if study == "`s'"
    local t = r(max)
  	forval w=1/`t' { //  look over each issue  
	   forval a=0/1 { // agree with party-candidate/disagree
			forval l=0/1 { //  issue knowledge scale_1!=4 & scale_2!=4 & scale_1!=4 & scale_2!=4 &
				tempvar tv1
				gene `tv1'=_n if scale_1!=4 & scale_2!=4 & agree == `a' & k_scale == `l' & study == "`s'" & t==`w'
				summ `tv1', meanonly
				local index=r(min)
				local mymacro=issues_scale[`index']
				drop `tv1'
				disp "`s' `mymacro' agree: `a' k_scale: `l'"
				sum scale_1 scale_2 if  agree == `a' & k_scale == `l' & study == "`s'" & t==`w'
				sum scale_1 scale_2 if scale_1!=4 & scale_2!=4 & agree == `a' & k_scale == `l' & study == "`s'" & t==`w'
				sum scale_1  if scale_1!=4 & scale_2!=4 & agree == `a' & k_scale == `l' & study == "`s'" & t==`w'
				local sd1 = r(sd)
				sum scale_2  if scale_1!=4 & scale_2!=4 & agree == `a' & k_scale == `l' & study == "`s'" & t==`w'
				local sd2 = r(sd)
				capture corr scale_1 scale_2 if  agree == `a' & k_scale == `l' & study == "`s'" & t==`w'
				local rho = .
				if _rc==0 {
				    local rho =r(rho)
				}
				post `memhold'  (`rho') (r(N) )  (`l') ("`s'")  (`a')  ("`mymacro'") (`sd1') (`sd2') ( `w')
		}
	  }
   }
}
postclose `memhold'


use Data/stability_pooled_corr_kak_std0ktype1fct0, clear //need to run the code for table 3
drop if regexm(study,"SSI")
drop if corr==.
egen maximum_number =count(corr) , by(study issue)
keep if maximum_number == 4 
egen minimum_N =min(N) , by(study issue)
tab study
 g issue_study = issue +" (" +study+")" 
*global studies  SSI1516 BES9295 BES9296 BES9297 Patterson Econ_7276 Econ_94_96 Econ_92_96 Econ_08 Env_08 Liberties_08 Immigration_08  All_92_96  All_08  BES_66_70
*SSI survey doesn't work with agreement versus disagreement because
*items followed ANES wording with two or three point scales, mostly agree versus disagree	
table  a k  ,c(mean corr )  f(%7.2f) // ** in paper  ************************************

tab study,g(study_n)
 replace study =regexr(study,"Econ_","")
 replace study =regexr(study,"All_","")
drop if study =="BES9295" 
drop if study =="BES9296" 
replace study ="ANES9296" if study== "92_96"
replace study ="ANES9296" if study== "92_96"
replace study ="ANES9496" if study== "94_96"
replace study ="ANES7276" if study== "7276"
replace study ="PAT76" if study== "76"
tab study
drop sd*
reshape wide corr N,i(issue study agree) j(k )
reshape wide corr0 corr1 N0 N1,i(issue study ) j(agree)
*drop if sample size is under 5 for either group
*Patterson questions are slightly different
tab issue if study=="PAT76"
replace issue = "Government jobs" if  issue=="jobs" & study=="PAT76"
replace issue = "Price controls" if issue=="price" & study=="PAT76"
replace issue = "Tax cut low incomes" if issue=="tax" & study=="PAT76"
replace issue = "Social welfare spending" if  issue=="spd" & study=="PAT76"
replace issue = "Taxes on high incomes" if issue=="tax"
replace issue = "Abortion" if issue=="abr"
replace issue = "Aid to Blacks" if issue=="aid"
replace issue = "Childcare spending" if issue=="childcare"
replace issue = "Debt ceiling" if issue=="debtceiling"
replace issue = "European integration" if issue=="eci"
replace issue = "Defense spending" if issue=="def"
replace issue = "Food stamps" if issue=="foodstamps"
replace issue = "Gov. services" if issue=="gss" || issue=="services" 
replace issue = "Food stamps" if issue=="foodstamps"
replace issue = "Equalize incomes" if issue=="ieq"
replace issue = "Nationalize industry" if issue=="prn"
replace issue = "Tax and spend more" if issue=="txs"
replace issue = "Employment vs. infl." if issue=="jbp"
replace issue = "Housing spending" if issue=="housing"
replace issue = "Busing" if issue=="bus"
replace issue = "Universal health care" if issue=="health"
replace issue = "Ideology" if issue=="ide"
replace issue = "Guaranteed jobs" if issue=="job" || issue=="jobs"
replace issue = "Medicare spending" if issue=="medicare"
replace issue = "Raise minimum wage" if issue=="minwage"
replace issue = "Parental leave" if issue=="parentleave"
replace issue = "Price controls" if issue=="price"
replace issue = "Marijuana legalization" if issue=="pot"
replace issue = "Rights of accused criminals" if issue=="rts"
replace issue = "School spending" if issue=="schools"
replace issue = "Social Security" if issue=="ss"
replace issue = "Taxes on high incomes" if issue=="tax"
replace issue = "Women's rights" if issue=="wmn"
tab issue
capture drop issue_study
g issue_study = issue +" (" +study+")" 
 destring study, replace

 /*
  graph dot corr00 corr10 ,yl(, labsize(small)) yt("Correlation between waves 1 and 2", size(small)) yl(-.5(.5)1) /// 
    over(issue_study, sort(corr00) lab(  labsize(tiny))) scheme(lean2) ///
	legend(label(2 "Place correctly") label(1 "Place incorrectly") size(small) tstyle(smbody)  rows(1) pos(12) ring(2))
        graph export Output\SIFigure_stability_disagree.png,width(5400)  height(4400)  replace
	graph dot corr01 corr11 ,yl(, labsize(small)) yt("Correlation between waves 1 and 2", size(small)) /// 
    over(issue_study, sort(corr01) lab(  labsize(tiny))) scheme(lean2)  yl(-.5(.5)1) ///
	legend(label(2 "Place correctly") label(1 "Place incorrectly") size(small) tstyle(smbody)  rows(1) pos(12) ring(2))
        graph export Output\SIFigure_stability_agree.png,width(5400)  height(4400)  replace
		*/
summ corr*
*corr00 don't know, don't agree
*corr11 know, agree
*figure three made in R: Figures_2_3.R 
save  Data/temporary/figure_3, replace






*************************************************************
********Figure 4 *   STABILITY BY PLACEMENT KNOWLEDGE AND NUMBER OF ITEMS IN THE SCALES
*ARS like plots pooling all studies  ******** 
*************************************************************
**Figure 4a
* 4 item scales
global factor 1 // 1 = factor scores vs simple average
global standardized 1 // 1 = means zero, standard deviation one
global missing = . // Set missing value. All coded to 7-point scales, so 4 is midpoint
                     // Need to remain consistent about that when adding new panels
global ktype 1 // 1 normal knowledge coding, 9 place them on the right sides of the scale 
global studies Econ_SSI1516  All_92_96 Econ_7276  Econ_BES9295 Econ_BES9296 Econ_BES9297 Econ_BES9701 Econ_76 Econ_94_96 
*do stability_pooled_coding_1_3.do // multi-item scale stability data set - no need to run if you did so above
use Data/stability_pooled_std${standardized}ktype${ktype}fct${factor}, clear
*tab total_knowledge
sum scale_1 scale_2 total_knowledge  k_scale
tempname memhold
postfile `memhold' t corr N ni tk str17 study  using Data/stability_pooled_corr_tk_std${standardized}ktype${ktype}fct${factor}  , replace	
foreach s in $studies {
	disp "`s'"
    sum t if study == "`s'"
    local z = r(max)
	sum total_knowledge if study == "`s'"
	local u = r(max)
	forval i=1/`z' { // tupples
	  forval l=0/`u' { // total issue knowledge
			  sum NUM_issues if total_knowledge == `l'  & t==`i' & study == "`s'"
			  local w = r(mean)
			  capture corr scale_1 scale_2 if total_knowledge == `l' & t==`i' & study == "`s'"
			   if _rc==0 {
				 post `memhold' (`i') (r(rho) ) (r(N) ) (`w') (`l') ("`s'") 
			   }
		   }
	}
}
postclose `memhold'
use Data/stability_pooled_corr_tk_std${standardized}ktype${ktype}fct${factor}, clear
	egen ni_max = max(ni), by(study) 
	keep if  ni_max ==4	
	g total_knowledge = ""
	for num 0/4: replace total_knowledge = "Correctly Place = X" if tk ==X
graph box  corr , yt("Correlation between waves 1 and 2",size(small))  over(ni,lab( labsize(small))) ///
    by(total_knowledge,b1("Number of items in the scale",size(small)) note("") iscale(*.8)) scheme(lean2) yl(0(.25)1, nogrid) 
	    graph export Output\figure_4a.png,width(3200)  height(2400)  replace
*Correlations used for table
table tk ni , c(mean corr)  format(%7.2f) 
* these correlations are then used in these calculations:
   *https://docs.google.com/spreadsheets/d/1DjCFQEcma-if1vJJDqBCwjBZY4wA7RtYQ0hv3vHGij0/edit?usp=sharing

**Figure 4b* SSI 
use Data/stability_pooled_corr_tk_std${standardized}ktype${ktype}fct${factor} if  study =="Econ_SSI1516", clear
g bin_knowledge = "Correctly Place = 0" if tk==0
replace bin_knowledge = "Correctly Place = 1-3" if tk==1
replace bin_knowledge = "Correctly Place = 4-6" if tk==2 
replace bin_knowledge = "Correctly Place = 7-10" if tk==3 
graph box  corr , yt("Correlation between waves 1 and 2",size(small))  over(ni,lab( labsize(small))) ///
    by(bin_knowledge,b1("Number of items in the scale",size(small)) note("") iscale(*.8)) scheme(lean2) yl(0(.25)1, nogrid) 
	    graph export Output\figure_4b.png,width(3200)  height(2400)  replace
